package com.tot.io;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by TOT on 2016/5/25.
 */
public class FileDemo {
    static String[] listFile(File dir,String fileNameRegex){
        List<String> files = new ArrayList<>();
        if(!dir.isDirectory()){
            throw new IllegalArgumentException("必须为目录");
        }
        if(fileNameRegex != null){
            FilenameFilter filenameFilter = new MyFileNameFilter(fileNameRegex);
            return dir.list(filenameFilter);
        } else {
            return dir.list();
        }

    }

    static void fileInfo(File file){
        if(file == null){
            throw new IllegalArgumentException("参数不能为空");
        }
        System.out.println(file.getName()+"的文件信息如下：\n"+
        "absolute path:"+file.getAbsolutePath()+
        "\n Can read:"+file.canRead()+
        "\n can wirte:"+file.canWrite()+
        "\n can excute:"+file.canExecute()+
        "\n lengh:"+file.length()+
        "\n last modified:"+file.lastModified()
        );
    }

    public static void main(String[] args) {

        File file = new File("C:\\Users\\TOT\\Documents\\SVN_OSCHINA\\plugindog\\pom.xml");
        fileInfo(file);
        System.out.println(file.getAbsolutePath());
        String[] strings = listFile(file,"(.)+.xml");
        System.out.println("=========>" + Arrays.toString(strings));
    }

    static class MyFileNameFilter implements FilenameFilter{
        private Pattern pattern;
        public MyFileNameFilter(String string){
            pattern = Pattern.compile(string);
        }

        @Override
        public boolean accept(File dir, String name) {
           return pattern.matcher(name).matches();
        }
    }

}
